Online-Academy
Look, Read, Understand, Apply

UI - Database

Connecting UI with mySql Database

Here, name of database = sawadee, user is root, and password = ''

import tkinter as tk
import mysql.connector

def click():
    name = cname.get()
    mob = mobile.get()
    add = address.get()
    sql = "insert into customers(cname,mobile,address) values(%s,%s,%s)"
    val = (name, mob, add)
    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="sawadee",
    )
    mycursor = mydb.cursor()
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    container = tk.Tk()  # creating container
    container.title("User Interface")  # giving title for the container
    container.geometry("300x400")  # specifying dimension of container (width, height)
    container.resizable(width=5, height=5)  # if zero resizing is not possible

    cname = tk.Entry(container, width=30)
    cname.grid(row=0, column=0)
    mobile = tk.Entry(container, width=30)
    mobile.grid(row=1, column=0)
    address = tk.Entry(container, width=30)
    address.grid(row=2, column=0)
    btn = tk.Button(container, text="Click", command=click)
    btn.grid(row=3, column=2)